package com.hifar.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Date;

import com.hifar.fw.utils.lang.DateUtils;

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TextFileUtil {
	
	public static void  addNewFileTechnicFile(String modulename, String logInfo,String suffix) {
		try {
			String currentPath = System.getProperty("user.dir");
			String logPath=currentPath+File.separator+"log";
			String logfilename=modulename+DateUtils.getTime14()+suffix;
			addContentToFile(logPath, logfilename,logInfo,"utf-8");
		} catch (Exception e) {
            log.error("写入日志文件信息错误！"+String.format("参数1：[%s] 参数2：[%s]", modulename,logInfo));
         
			e.printStackTrace();
		}
		
	}
	public static void  addContent(String modulename, String logInfo) {
		try {
			String currentPath = System.getProperty("user.dir");
			String logPath=currentPath+File.separator+"log";
			String logfilename=modulename+DateUtils.getTime8()+".txt";
			addContentToFile(logPath, logfilename, DateUtils.formatTime(new Date(),"yyyy-MM-dd_HH:mm:ss")+"("+logInfo+")");
		} catch (Exception e) {
            log.error("写入日志文件信息错误！"+String.format("参数1：[%s] 参数2：[%s]", modulename,logInfo));
         
			e.printStackTrace();
		}
		
	}

	public static void  addContentNoTime(String modulename, String logInfo) {
		try {
			String currentPath = System.getProperty("user.dir");
			String logPath=currentPath+File.separator+"log";
			String logfilename=modulename+DateUtils.getTime8()+".txt";
			addContentToFile(logPath, logfilename, logInfo);
		} catch (Exception e) {
            log.error("写入日志文件信息错误！"+String.format("参数1：[%s] 参数2：[%s]", modulename,logInfo));
         
			e.printStackTrace();
		}
		
	}
	
	
	
	public static void addContentToFile(String logFileDirecoty, String logFileName, String logInfo) {
		// 创建目录
		File logDirectory = new File(logFileDirecoty);
		if (!logDirectory.exists()) {
			logDirectory.mkdirs();
		}
		File logFile = new File(logDirectory+File.separator+logFileName);
		BufferedWriter out = null;
		FileOutputStream fileOutPutStream = null;
		try {
			fileOutPutStream = new FileOutputStream(logFile, true);
			out = new BufferedWriter(new OutputStreamWriter(fileOutPutStream, "UTF-8"));
			out.write(logInfo + "\n");
			out.close();
			fileOutPutStream.close();
			out = null;
			fileOutPutStream = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fileOutPutStream != null) {
					fileOutPutStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			out = null;
			fileOutPutStream = null;
		}

	}
	
	public static void addContentToFile(String logFileDirecoty, String logFileName, String logInfo,String charSet) {
		// 创建目录
		File logDirectory = new File(logFileDirecoty);
		if (!logDirectory.exists()) {
			logDirectory.mkdirs();
		}
		File logFile = new File(logDirectory+File.separator+logFileName);
		BufferedWriter out = null;
		FileOutputStream fileOutPutStream = null;
		try {
			fileOutPutStream = new FileOutputStream(logFile, true);
			//  StandardCharsets.UTF_8
			out = new BufferedWriter(new OutputStreamWriter(fileOutPutStream, charSet));
			out.write(logInfo + "\n");
			out.close();
			fileOutPutStream.close();
			out = null;
			fileOutPutStream = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fileOutPutStream != null) {
					fileOutPutStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			out = null;
			fileOutPutStream = null;
		}

	}
}
